﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WMS.BLL;
using WMS.Entity;

namespace WMS.App
{
    public partial class frmInventoryGoods : Form
    {
        private Goods goods = null;
        private frmIntoOutGoodsLog fGoodsRecords = null;
        private frmGoodsInfo fGoodsInfo = null;

        public frmInventoryGoods()
        {
            InitializeComponent();

            InitData();
        }

        public void InitData()
        {
            goods = new Goods();

            fGoodsRecords = new frmIntoOutGoodsLog();
            fGoodsInfo = new frmGoodsInfo();

            this.dataGridView1.DataSource = new List<GoodsEntity>();
            this.txtName.Text = "";
            this.txtName.Focus();
        }

        public bool AddInGoodsLog
        {
            get;
            set;
        }

        private void frmInventoryGoods_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtName.Text))
            {
                this.dataGridView1.DataSource = goods.GetListByName(this.txtName.Text);
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (e.ColumnIndex == 0)
                {
                    var goodsID = (int)(this.dataGridView1.Rows[e.RowIndex].Cells["GoodsID"].Value);
                    fGoodsInfo.FillDataToForm(goodsID);
                    fGoodsInfo.ShowDialog();
                    this.btnQuery_Click(null, null);
                }
                else if (e.ColumnIndex == 1)
                {
                    var goodsID = this.dataGridView1.Rows[e.RowIndex].Cells["GoodsID"].Value.ToString();
                    var goodsName = this.dataGridView1.Rows[e.RowIndex].Cells["ProName"].Value.ToString();
                    fGoodsRecords.SetGoodsLog(goodsName, goodsID, AddInGoodsLog ? 0 : 1);
                    fGoodsRecords.ShowDialog();
                }
            }
        }
    }
}
